package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author Andrew Tolver
	 */
	public class MovingObject extends FlxSprite
	{
		private var _boundBox:FlxRect;		//stores moveable area rectangle
		
		/**
		 * Constructor for the moving object
		 * 
		 * @param	image		Image you want to use for the object.
		 * @param	oWidth		Width of the object.
		 * @param	oHeight		Height of the object.
		 */
		public function MovingObject(image:Class, oWidth:Number, oHeight:Number) 
		{
			super();
			loadGraphic(image, false, false, oWidth, oHeight);
			_boundBox = new FlxRect();
			this.fixed = true;
			this.collideLeft = false;
			this.collideRight = false;
			this.collideBottom = false;
		}
		
		/**
		 * Give the object a starting position.
		 * 
		 * @param	X		Starting X point of the object.
		 * @param	Y		Starting Y point of the object.
		 */
		public function setStartPoint(X:Number = 0, Y:Number = 0):void
		{
			this.x = X;
			this.y = Y;
		}
		
		/**
		 * Instantiate an ending point for the object.  This assumes the box is at the starting point.
		 * 
		 * @param	X		The X-coordinate of the ending point in space.
		 * @param	Y		The Y-coordinate of the ending point in space.
		 */
		public function setBoundingBox(X:Number, Y:Number):void
		{
			var endPointX:Number;
			var endPointY:Number;
			var startPointX:Number;
			var startPointY:Number;
			
			if (X > this.x)
			{
				endPointX = X;
				startPointX = this.x
			}
			else
			{
				endPointX = this.x;
				startPointX = X;
			}
			if (Y > this.y)
			{
				endPointY = Y;
				startPointY = this.y;
			}
			else
			{
				endPointY = this.y;
				startPointY = Y;
			}
			
			_boundBox.x = startPointX;
			_boundBox.y = startPointY;
			_boundBox.width = endPointX - startPointX;
			_boundBox.height = endPointY - startPointY;
		}
		
		/**
		 * Instantiate an ending point for the object.  This assumes the box is at the starting point.
		 * 
		 * @param	leftPoint		The left of the bounding box
		 * @param	rightPoint		The right of the bounding box
		 * @param	topPoint		The top of the bounding box
		 * @param	bottomPoint		The bottom of the bounding box
		 */
		public function setCirclingBox(leftPoint:Number, rightPoint:Number, topPoint:Number, bottomPoint:Number):void
		{
			_boundBox.x = leftPoint;
			_boundBox.width = rightPoint - leftPoint;
			_boundBox.y = topPoint;
			_boundBox.height = bottomPoint - topPoint;
		}
		
		/**
		 * Give the object velocity values to move.
		 * 
		 * @param	velocityX		The speed of the object in the X plane.
		 * @param	velocityY		The speed of the object in the Y plane.
		 */
		public function setVelocity(velocityX:Number, velocityY:Number):void
		{
			velocity.x = velocityX;
			velocity.y = velocityY;
			if (_boundBox.width == 0)
			{
				velocity.x = 0;
			}
			if (_boundBox.height == 0)
			{
				velocity.y = 0;
			}
		}
		
		override public function update():void
		{
			super.update();
			
			if (this.y > _boundBox.bottom && velocity.y > 0)
			{
				velocity.y = -velocity.y;
			}
			if (this.y < _boundBox.top && velocity.y < 0)
			{
				velocity.y = -velocity.y;
			}
			
			if (this.x > _boundBox.right && velocity.x > 0)
			{
				velocity.x = -velocity.x;
			}
			if (this.x < _boundBox.left && velocity.x < 0)
			{
				velocity.x = -velocity.x;
			}
		}
		
	}

}